package com.prs.crm.action.oa;

import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.AnnouncementReceive;
import com.prs.crm.domain.oa.AttendanceManagement;
import com.prs.crm.domain.oa.CheckWork;
import com.prs.crm.domain.oa.CompanyMailList;
import com.prs.crm.domain.oa.GoOut;
import com.prs.crm.domain.oa.PersonTime;
import com.prs.crm.domain.sys.CheckingSet;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.service.ReportService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.oa.AttendanceManagementService;
import com.prs.crm.service.oa.CheckWorkService;
import com.prs.crm.service.oa.PersonTimeService;
import com.prs.crm.service.sys.CheckingSetService;

public class AttendanceManagementAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private AttendanceManagement attendanceManagement;
	private AttendanceManagementService attendanceManagementService;
	private PersonTime personTime;
	private PersonTimeService personTimeService;
	private Integer[] timeIds;
	private CheckWork checkWork;
	private CheckWorkService checkWorkService;
	private String accepts;
	private PersonService personService;
	private String time;
	private String data;
	private ReportService reportService;
	private CheckingSet checkingSet;
    private CheckingSetService checkingSetService;
	
	private InputStream inputStream;
	
	private String contentType;
	
	private String outFilePath;
	
	private String outfileName;
	
	private int exportType;
	private Integer persons;
	public Integer getPersons() {
		return persons;
	}
	public void setPersons(Integer persons) {
		this.persons = persons;
	}
	@Action("listAttendanceManagement")
	public String listAttendanceManagement() {	
		return SUCCESS;
	}
	@Action("editTimePer")
	public String editTimePer() throws ParseException {
//		if(attendanceManagement==null){
//			this.attendanceManagement=this.attendanceManagementService.get(1);
//		}
//		Person person=AppContext.getAccountContext().currentUser();
//		this.checkWork=this.checkWorkService.getCheckWorkByPerson(person);
//		//System.out.println(checkWork.getId());
//		if(checkWork==null){
//			this.personTime=this.personTimeService.getPersonTimeByPerson(person);
//			checkWork=new CheckWork();
//			checkWork.setRecordTime2(new Date());
//			this.checkWorkService.save(checkWork);
//		}
		this.checkingSet = this.getCheckingSetService().getCheckingSet("cs01");
		return SUCCESS;
	}
	@Action("editPersonTime")
	public String editPersonTime() {
		if(this.timeIds!=null){
			this.personTime=this.personTimeService.get(timeIds[0]);
		}
		return SUCCESS;
	}
	@Action("listCheckWork")
	public String listCheckWork(){
//		if(attendanceManagement==null){
//			this.attendanceManagement=this.attendanceManagementService.get(1);
//		}
		this.checkingSet = this.getCheckingSetService().getCheckingSet("cs01");
//		attendanceManagement.setIsNum(checkingSet.getTimeModel());
//		this.getAttendanceManagementService().save(attendanceManagement);
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CheckWork.class);
		criteria.addOrder(Order.desc("recordTime"));
		criteria.add(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.checkWorkService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listCheckWorks")
	public String listCheckWorks(){
		this.checkingSet = this.getCheckingSetService().getCheckingSet("cs01");
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CheckWork.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.checkWorkService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("listPersonTime")
	public String listPersonTime(){
		Person person=AppContext.getAccountContext().currentUser();
		if(person!=null){
			this.personTime=this.personTimeService.getPersonTimeByPerson(person);
			if(personTime==null){
				personTime = new PersonTime();
				System.out.println(personTime);
				personTime.setMorningInTime(attendanceManagement.getMorningInTime());
				personTime.setMorningOutTime(attendanceManagement.getMorningOutTime());
				personTime.setAfternoonInTime(attendanceManagement.getAfternoonInTime());
				personTime.setAfternoonOutTime(attendanceManagement.getAfternoonOutTime());
				personTime.setRecorder(person);
				this.personTimeService.save(personTime);
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PersonTime.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.personTimeService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editAttendanceManagementDefaultTime")
	public String editAttendanceManagementDefaultTime() {
		if(attendanceManagement==null){
			this.attendanceManagement=this.attendanceManagementService.get(1);
		}
		return SUCCESS;
	}
	@Action("editAttendanceManagement")
	public String editAttendanceManagement() {
		if(attendanceManagement==null){
		this.attendanceManagement=this.attendanceManagementService.get(1);
		}
		this.checkingSet = this.getCheckingSetService().getCheckingSet("cs01");
		attendanceManagement.setIsNum(checkingSet.getTimeModel());
		this.getAttendanceManagementService().save(attendanceManagement);
		return SUCCESS;
	}
	@Action(value = "updateAttendanceManagement", results = {
			@Result(name = "success", type = "redirect", location = "editAttendanceManagement?attendanceManagement=${attendanceManagement.getId()}"),
			@Result(name = "error", type = "chain", location = "editAttendanceManagement?attendanceManagement=${attendanceManagement.getId()}") })
	public String updateAttendanceManagement() {
		this.attendanceManagementService.submit(attendanceManagement);
		return SUCCESS;
	}
	@Action(value = "saveAttendanceManagement", results = {
			@Result(name = "success", type = "redirect", location = "editAttendanceManagement?attendanceManagement=${attendanceManagement.getId()}"),
			@Result(name = "error", type = "chain", location = "editAttendanceManagementDefaultTime?attendanceManagement=${attendanceManagement.getId()}") })
	public String saveAttendanceManagement() {
		this.attendanceManagementService.save(attendanceManagement);
		return SUCCESS;
	}
	@Action(value = "savePersonTime", results = {
			@Result(name = "success", type = "redirect", location = "listPersonTime?attendanceManagement=${attendanceManagement.getId()}"),
			@Result(name = "error", type = "chain", location = "editPersonTime?personTime=${personTime.getId()}&attendanceManagement=${attendanceManagement.getId()}") })
	public String savePersonTime() {
		this.personTimeService.save(personTime);
		return SUCCESS;
	}
	@Action(value = "saveDefaultTime", results = {
			@Result(name = "success", type = "redirect", location = "listPersonTime?attendanceManagement=${attendanceManagement.getId()}"),
			@Result(name = "error", type = "chain", location = "listPersonTime?attendanceManagement=${attendanceManagement.getId()}") })
	public String saveDefaultTime() {
		personTime.setMorningInTime(attendanceManagement.getMorningInTime());
		personTime.setMorningOutTime(attendanceManagement.getMorningOutTime());
		personTime.setAfternoonInTime(attendanceManagement.getAfternoonInTime());
		personTime.setAfternoonOutTime(attendanceManagement.getAfternoonOutTime());
		this.personTimeService.submit(personTime);
		return SUCCESS;
	}
	@Action(value = "saveInTime", results = {
			@Result(name = "success", type = "redirect", location = "editTimePer?checkWork=${checkWork.getId()}"),
			@Result(name = "error", type = "chain", location = "editTimePer?checkWork=${checkWork.getId()}") })
	public String saveInTime() throws ParseException {
		persons=AppContext.getAccountContext().currentUser().getId();
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
		Date t=new SimpleDateFormat("yyyy-MM-dd").parse(time);
		Date d=new SimpleDateFormat("HH:mm").parse(data);
		SimpleDateFormat sf1=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf2=new SimpleDateFormat("HH:mm");
		String d1=sf1.format(date);
		String d2=sf2.format(d);
		Date d3=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(d1+" "+d2);
		SimpleDateFormat sfs=new SimpleDateFormat("yyyy-MM-dd");
		String time=sfs.format(date)+" "+checkingSet.getWorkTime();
		Date d4=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
			Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(accepts.split(",")));
			Iterator<Person> iter = acceptPersons.iterator();
			while(iter.hasNext()) {
				Person p = iter.next();
				this.checkWork=this.checkWorkService.getCheckWorkByPerson(p);
				if (checkWork==null) {
					checkWork=new CheckWork();
					checkWork.setRecorder(p);
					checkWork.setRecordTime2(new Date());
					checkWork.setMorningInTime(d);
					checkWork.setCode(sf.format(date));
					checkWork.setPersons(persons);
					this.checkWorkService.save(checkWork);
					if (d3.after(d4)) {
						checkWork.setMorState1(1);
						if (checkWork.getRemark()==null || "迟到".equals(checkWork.getRemark())) {						
							checkWork.setRemark("迟到");
						}else{
							checkWork.setRemark(checkWork.getRemark()+" 迟到");
						}
						this.getCheckWorkService().submit(checkWork);
					}
				}else{
					CheckWork checkWorks=this.getCheckWorkService().getCode(sf.format(t),persons);
					if (checkWorks==null) {
						checkWorks=new CheckWork();
						checkWorks.setRecordTime2(new Date());
						checkWorks.setMorningInTime(d);
						checkWorks.setPersons(persons);
						this.checkWorkService.save(checkWorks);
						if (d3.after(d4)) {
							checkWork.setMorState1(1);
							if (checkWorks.getRemark()==null || "迟到".equals(checkWorks.getRemark())) {
								checkWorks.setRemark("迟到");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 迟到");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
					}else{
						checkWorks.setRecordTime2(new Date());
						checkWorks.setMorningInTime(d);
						this.checkWorkService.submit(checkWorks);
						if (d3.after(d4)) {
							checkWork.setMorState1(1);
							if (checkWorks.getRemark()==null || "迟到".equals(checkWorks.getRemark())) {
								checkWorks.setRemark("迟到");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 迟到");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
						
					}
				}		
			}
		return SUCCESS;
	}
	@Action(value = "saveOutTime", results = {
			@Result(name = "success", type = "redirect", location = "editTimePer?attendanceManagement=${attendanceManagement.getId()}"),
			@Result(name = "error", type = "chain", location = "editTimePer?attendanceManagement=${attendanceManagement.getId()}") })
	public String saveOutTime() throws ParseException {
		persons=AppContext.getAccountContext().currentUser().getId();
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sfs=new SimpleDateFormat("yyyy-MM-dd");
		Date t=new SimpleDateFormat("yyyy-MM-dd").parse(time);
		Date d=new SimpleDateFormat("HH:mm").parse(data);
		SimpleDateFormat sf1=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf2=new SimpleDateFormat("HH:mm");
		String d1=sf1.format(date);
		String d2=sf2.format(d);
		Date d3=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(d1+" "+d2);
		String time=sfs.format(date)+" "+checkingSet.getClosedTime();
		Date d4=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
			Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(accepts.split(",")));
			Iterator<Person> iter = acceptPersons.iterator();
			while(iter.hasNext()) {
				Person p = iter.next();
				this.checkWork=this.checkWorkService.getCheckWorkByPerson(p);
				if (checkWork==null) {
					checkWork=new CheckWork();
					checkWork.setRecorder(p);
					checkWork.setRecordTime2(new Date());
					checkWork.setAfternoonOutTime(d);
					checkWork.setCode(sf.format(date));
					checkWork.setPersons(persons);
					this.checkWorkService.save(checkWork);
					if (d3.before(d4)) {
						checkWork.setAftState2(1);
						if (checkWork.getRemark()==null ||"早退".equals(checkWork.getRemark())) {
							checkWork.setRemark("早退");
						}else{
							checkWork.setRemark(checkWork.getRemark()+" 早退");
						}
						this.getCheckWorkService().submit(checkWork);
					}
				}else{
					CheckWork checkWorks=this.getCheckWorkService().getCode(sf.format(t),persons);
					if (checkWorks==null) {
						checkWorks=new CheckWork();
						checkWorks.setRecordTime2(new Date());
						checkWorks.setAfternoonOutTime(d);
						checkWorks.setPersons(persons);
						this.checkWorkService.save(checkWorks);
						if (d3.before(d4)) {
							checkWork.setAftState2(1);
							if (checkWorks.getRemark()==null ||"早退".equals(checkWork.getRemark())) {
								checkWorks.setRemark("早退");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 早退");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
					}else{
						checkWorks.setRecordTime2(new Date());
						checkWorks.setAfternoonOutTime(d);
						this.checkWorkService.submit(checkWorks);
						if (d3.before(d4)) {
							checkWork.setAftState2(1);
							if (checkWorks.getRemark()==null ||"早退".equals(checkWork.getRemark())) {
								checkWorks.setRemark("早退");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 早退");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
						
					}
				}		
			}
		return SUCCESS;
	}
	@Action(value = "saveMorningInTime", results = {
			@Result(name = "success", type = "redirect", location = "editTimePer?attendanceManagement=${attendanceManagement.getId()}"),
			@Result(name = "error", type = "chain", location = "editTimePer?attendanceManagement=${attendanceManagement.getId()}") })
	public String saveMorningInTime() throws ParseException {
		persons=AppContext.getAccountContext().currentUser().getId();
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
		Date t=new SimpleDateFormat("yyyy-MM-dd").parse(time);
		Date d=new SimpleDateFormat("HH:mm").parse(data);
		SimpleDateFormat sf1=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf2=new SimpleDateFormat("HH:mm");
		String d1=sf1.format(date);
		String d2=sf2.format(d);
		Date d3=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(d1+" "+d2);
		SimpleDateFormat sfs=new SimpleDateFormat("yyyy-MM-dd");
		String time=sfs.format(date)+" "+checkingSet.getForenoonWorkTime();
		Date d4=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
			Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(accepts.split(",")));
			Iterator<Person> iter = acceptPersons.iterator();
			while(iter.hasNext()) {
				Person p = iter.next();
				this.checkWork=this.checkWorkService.getCheckWorkByPerson(p);
				if (checkWork==null) {
					checkWork=new CheckWork();
					checkWork.setRecorder(p);
					checkWork.setRecordTime2(new Date());
					checkWork.setMorningInTime(d);
					checkWork.setCode(sf.format(date));
					checkWork.setPersons(persons);
					this.checkWorkService.save(checkWork);
					if (d3.after(d4)) {
						checkWork.setMorState1(1);
						if (checkWork.getRemark()==null || "迟到".equals(checkWork.getRemark())) {
							checkWork.setRemark("迟到");
						}else{
							checkWork.setRemark(checkWork.getRemark()+" 迟到");
						}
						this.getCheckWorkService().submit(checkWork);
					}
				}else{
					CheckWork checkWorks=this.getCheckWorkService().getCode(sf.format(t),persons);
					if (checkWorks==null) {
						checkWorks=new CheckWork();
						checkWorks.setRecordTime2(new Date());
						checkWorks.setMorningInTime(d);
						checkWorks.setPersons(persons);
						this.checkWorkService.save(checkWorks);
						if (d3.after(d4)) {
							checkWork.setMorState1(1);
							if (checkWorks.getRemark()==null || "迟到".equals(checkWorks.getRemark())) {
								checkWorks.setRemark("迟到");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 迟到");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
					}else{
						checkWorks.setRecordTime2(new Date());
						checkWorks.setMorningInTime(d);
						this.checkWorkService.submit(checkWorks);
						if (d3.after(d4)) {
							checkWork.setMorState1(1);
							if (checkWorks.getRemark()==null || "迟到".equals(checkWorks.getRemark())) {
								checkWorks.setRemark("迟到");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 迟到");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
						
					}
				}		
			}
		return SUCCESS;
	}
	@Action(value = "saveMorningOutTime", results = {
			@Result(name = "success", type = "redirect", location = "editTimePer?attendanceManagement=${attendanceManagement.getId()}"),
			@Result(name = "error", type = "chain", location = "editTimePer?attendanceManagement=${attendanceManagement.getId()}") })
	public String saveMorningOutTime() throws ParseException {
		persons=AppContext.getAccountContext().currentUser().getId();
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sfs=new SimpleDateFormat("yyyy-MM-dd");
		Date t=new SimpleDateFormat("yyyy-MM-dd").parse(time);
		Date d=new SimpleDateFormat("HH:mm").parse(data);
		SimpleDateFormat sf1=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf2=new SimpleDateFormat("HH:mm");
		String d1=sf1.format(date);
		String d2=sf2.format(d);
		Date d3=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(d1+" "+d2);
		String time=sfs.format(date)+" "+checkingSet.getForenoonClosdTime();
		Date d4=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
			Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(accepts.split(",")));
			Iterator<Person> iter = acceptPersons.iterator();
			while(iter.hasNext()) {
				Person p = iter.next();
				this.checkWork=this.checkWorkService.getCheckWorkByPerson(p);
				if (checkWork==null) {
					checkWork=new CheckWork();
					checkWork.setRecorder(p);
					checkWork.setRecordTime2(new Date());
					checkWork.setMorningOutTime(d);
					checkWork.setCode(sf.format(date));
					checkWork.setPersons(persons);
					this.checkWorkService.save(checkWork);
					if (d3.before(d4)) {
						checkWork.setMorState2(1);
						if (checkWork.getRemark()==null ||"早退".equals(checkWork.getRemark())) {
							checkWork.setRemark("早退");
						}else{
							checkWork.setRemark(checkWork.getRemark()+" 早退");
						}
						this.getCheckWorkService().submit(checkWork);
					}
				}else{
					CheckWork checkWorks=this.getCheckWorkService().getCode(sf.format(t),persons);
					if (checkWorks==null) {
						checkWorks=new CheckWork();
						checkWorks.setRecordTime2(new Date());
						checkWorks.setMorningOutTime(d);
						checkWorks.setPersons(persons);
						this.checkWorkService.save(checkWorks);
						if (d3.before(d4)) {
							checkWork.setMorState2(1);
							if (checkWorks.getRemark()==null ||"早退".equals(checkWork.getRemark())) {
								checkWorks.setRemark("早退");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 早退");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
					}else{
						checkWorks.setRecordTime2(new Date());
						checkWorks.setMorningOutTime(d);
						this.checkWorkService.submit(checkWorks);
						if (d3.before(d4)) {
							checkWork.setMorState2(1);
							if (checkWorks.getRemark()==null ||"早退".equals(checkWork.getRemark())) {
								checkWorks.setRemark("早退");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 早退");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
						
					}
				}		
			}
		return SUCCESS;
	}
	@Action(value = "saveAfernoonInTime", results = {
			@Result(name = "success", type = "redirect", location = "editTimePer?attendanceManagement=${attendanceManagement.getId()}"),
			@Result(name = "error", type = "chain", location = "editTimePer?attendanceManagement=${attendanceManagement.getId()}") })
	public String saveAfernoonInTime() throws ParseException {
		persons=AppContext.getAccountContext().currentUser().getId();
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
		Date t=new SimpleDateFormat("yyyy-MM-dd").parse(time);
		Date d=new SimpleDateFormat("HH:mm").parse(data);
		SimpleDateFormat sf1=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf2=new SimpleDateFormat("HH:mm");
		String d1=sf1.format(date);
		String d2=sf2.format(d);
		Date d3=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(d1+" "+d2);
		SimpleDateFormat sfs=new SimpleDateFormat("yyyy-MM-dd");
		String time=sfs.format(date)+" "+checkingSet.getAfternoonWorkTime();
		Date d4=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
			Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(accepts.split(",")));
			Iterator<Person> iter = acceptPersons.iterator();
			while(iter.hasNext()) {
				Person p = iter.next();
				this.checkWork=this.checkWorkService.getCheckWorkByPerson(p);
				if (checkWork==null) {
					checkWork=new CheckWork();
					checkWork.setRecorder(p);
					checkWork.setPersons(persons);
					checkWork.setRecordTime2(new Date());
					checkWork.setAfternoonInTime(d);
					checkWork.setCode(sf.format(date));
					this.checkWorkService.save(checkWork);
					if (d3.after(d4)) {
						checkWork.setAftState1(1);
						if (checkWork.getRemark()==null || "迟到".equals(checkWork.getRemark())) {
							checkWork.setRemark("迟到");
						}else{
							checkWork.setRemark(checkWork.getRemark()+" 迟到");
						}
						this.getCheckWorkService().submit(checkWork);
					}
				}else{
					CheckWork checkWorks=this.getCheckWorkService().getCode(sf.format(t),persons);
					if (checkWorks==null) {
						checkWorks=new CheckWork();
						checkWorks.setRecordTime2(new Date());
						checkWorks.setAfternoonInTime(d);
						checkWorks.setPersons(persons);
						this.checkWorkService.save(checkWorks);
						
						if (d3.after(d4)) {
							checkWork.setAftState1(1);
							if (checkWorks.getRemark()==null || "迟到".equals(checkWorks.getRemark())) {
								checkWorks.setRemark("迟到");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 迟到");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
					}else{
						checkWorks.setRecordTime2(new Date());
						checkWorks.setAfternoonInTime(d);
						this.checkWorkService.submit(checkWorks);
						if (d3.after(d4)) {
							checkWork.setAftState1(1);
							if (checkWorks.getRemark()==null || "迟到".equals(checkWorks.getRemark())) {
								checkWorks.setRemark("迟到");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 迟到");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
						
					}
				}		
			}
		return SUCCESS;
	}
	@Action(value = "saveAfernoonOutTime", results = {
			@Result(name = "success", type = "redirect", location = "editTimePer?attendanceManagement=${attendanceManagement.getId()}"),
			@Result(name = "error", type = "chain", location = "editTimePer?attendanceManagement=${attendanceManagement.getId()}") })
	public String saveAfernoonOutTime() throws ParseException {
		persons=AppContext.getAccountContext().currentUser().getId();
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sfs=new SimpleDateFormat("yyyy-MM-dd");
		Date t=new SimpleDateFormat("yyyy-MM-dd").parse(time);
		Date d=new SimpleDateFormat("HH:mm").parse(data);
		SimpleDateFormat sf1=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf2=new SimpleDateFormat("HH:mm");
		String d1=sf1.format(date);
		String d2=sf2.format(d);
		Date d3=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(d1+" "+d2);
		String time=sfs.format(date)+" "+checkingSet.getAfternoonClosdTime();
		Date d4=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
			Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(accepts.split(",")));
			Iterator<Person> iter = acceptPersons.iterator();
			while(iter.hasNext()) {
				Person p = iter.next();
				this.checkWork=this.checkWorkService.getCheckWorkByPerson(p);
				if (checkWork==null) {
					checkWork=new CheckWork();
					checkWork.setRecorder(p);
					checkWork.setRecordTime2(new Date());
					checkWork.setAfternoonOutTime(d);
					checkWork.setCode(sf.format(date));
					checkWork.setPersons(persons);
					this.checkWorkService.save(checkWork);
					if (d3.before(d4)) {
						checkWork.setAftState2(1);
						if (checkWork.getRemark()==null ||"早退".equals(checkWork.getRemark())) {
							checkWork.setRemark("早退");
						}else{
							checkWork.setRemark(checkWork.getRemark()+" 早退");
						}
						this.getCheckWorkService().submit(checkWork);
					}
				}else{
					CheckWork checkWorks=this.getCheckWorkService().getCode(sf.format(t),persons);
					if (checkWorks==null) {
						checkWorks=new CheckWork();
						checkWorks.setRecordTime2(new Date());
						checkWorks.setAfternoonOutTime(d);
						checkWorks.setPersons(persons);
						this.checkWorkService.save(checkWorks);
						if (d3.before(d4)) {
							checkWork.setAftState2(1);
							if (checkWorks.getRemark()==null ||"早退".equals(checkWork.getRemark())) {
								checkWorks.setRemark("早退");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 早退");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
					}else{
						checkWorks.setRecordTime2(new Date());
						checkWorks.setAfternoonOutTime(d);
						this.checkWorkService.submit(checkWorks);
						if (d3.before(d4)) {
							checkWork.setAftState2(1);
							if (checkWorks.getRemark()==null ||"早退".equals(checkWork.getRemark())) {
								checkWorks.setRemark("早退");
							}else{
								checkWorks.setRemark(checkWorks.getRemark()+" 早退");
							}
							this.getCheckWorkService().submit(checkWorks);
						}
						
					}
				}		
			}
		return SUCCESS;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public AttendanceManagement getAttendanceManagement() {
		return attendanceManagement;
	}

	public void setAttendanceManagement(AttendanceManagement attendanceManagement) {
		this.attendanceManagement = attendanceManagement;
	}

	public AttendanceManagementService getAttendanceManagementService() {
		return attendanceManagementService;
	}

	public void setAttendanceManagementService(
			AttendanceManagementService attendanceManagementService) {
		this.attendanceManagementService = attendanceManagementService;
	}
	public PersonTime getPersonTime() {
		return personTime;
	}
	public void setPersonTime(PersonTime personTime) {
		this.personTime = personTime;
	}
	public PersonTimeService getPersonTimeService() {
		return personTimeService;
	}
	public void setPersonTimeService(PersonTimeService personTimeService) {
		this.personTimeService = personTimeService;
	}
	public Integer[] getTimeIds() {
		return timeIds;
	}
	public void setTimeIds(Integer[] timeIds) {
		this.timeIds = timeIds;
	}
	public CheckWork getCheckWork() {
		return checkWork;
	}
	public void setCheckWork(CheckWork checkWork) {
		this.checkWork = checkWork;
	}
	public CheckWorkService getCheckWorkService() {
		return checkWorkService;
	}
	public void setCheckWorkService(CheckWorkService checkWorkService) {
		this.checkWorkService = checkWorkService;
	}
	public String getAccepts() {
		return accepts;
	}
	public void setAccepts(String accepts) {
		this.accepts = accepts;
	}
	
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
	public ReportService getReportService() {
		return reportService;
	}
	public void setReportService(ReportService reportService) {
		this.reportService = reportService;
	}
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getContentType() {
		return contentType;
	}
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	public String getOutFilePath() {
		return outFilePath;
	}
	public void setOutFilePath(String outFilePath) {
		this.outFilePath = outFilePath;
	}
	public String getOutfileName() {
		return outfileName;
	}
	public void setOutfileName(String outfileName) {
		this.outfileName = outfileName;
	}
	public int getExportType() {
		return exportType;
	}
	public void setExportType(int exportType) {
		this.exportType = exportType;
	}
	public CheckingSet getCheckingSet() {
		return checkingSet;
	}
	public void setCheckingSet(CheckingSet checkingSet) {
		this.checkingSet = checkingSet;
	}
	public CheckingSetService getCheckingSetService() {
		return checkingSetService;
	}
	public void setCheckingSetService(CheckingSetService checkingSetService) {
		this.checkingSetService = checkingSetService;
	}
	
}
